package org.ufinity.hsms.module.device;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;
import org.ufinity.hsms.R;
import org.ufinity.hsms.common.activity.BaseActivity;
import org.ufinity.hsms.common.application.HSMApplication;
import org.ufinity.hsms.common.command.AbstractCommand;
import org.ufinity.hsms.common.command.CommandService;
import org.ufinity.hsms.common.command.ICallBack;
import org.ufinity.hsms.common.utils.InfoHelper;
import org.ufinity.hsms.module.TabGroupActivity;
import org.ufinity.hsms.module.device.adapter.ItemAdapter;
import org.ufinity.hsms.module.device.command.ItemCommand;
import org.ufinity.hsms.module.device.entity.ApplicationEntity;
import org.ufinity.hsms.module.device.entity.ItemEntity;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;

public class ItemActivity extends BaseActivity implements OnClickListener, OnItemClickListener{
	public static final String TAG = "ItemActivity";
	private HSMApplication application;
	ProgressDialog proDialog = null;
	ListView listView;
	TextView txt_title;
	ItemAdapter adapter;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		instance = this;
		application = getBaseApplication();
		application.addActivity(instance);
		setContentView(R.layout.device);
		initAapter();
		initView();
		initDate();
	}
	
	
	private void initAapter() {
		adapter = new ItemAdapter(this);
	}
	
	
	private void initView() {
		listView = (ListView) findViewById(R.id.listView);
		listView.setAdapter(adapter);
		listView.setOnItemClickListener(this);
		txt_title = (TextView) findViewById(R.id.txt_title);
		txt_title.setText(getResources().getString(R.string.items));
		proDialog = new ProgressDialog(getParent());
		proDialog.setMessage(getResources().getString(R.string.validate));
		proDialog.setIndeterminate(false);
		proDialog.setCancelable(true);
	}
	
	public void refresh(View v) {
		initDate();
	}
	
	private void initDate() {
		if (!InfoHelper.checkNetWork(mContext)) {
			makeMyToast(mContext, getResources().getString(
					R.string.connect_error));
			return;
		}else{
			ApplicationEntity applicationEntity = (ApplicationEntity)getIntent().getExtras().get("application");
			HashMap<String, String> map = new HashMap<String,String>();
			map.put("applicationId", applicationEntity.getApplicationid());
			map.put("hostId", applicationEntity.getHostId());
			proDialog.show();
			ItemCommand cmd = new ItemCommand(map);
			cmd.addCallBack("item",new ICallBack<JSONObject>() {
				
				@Override
				public void callBack(AbstractCommand<JSONObject> command,
						JSONObject json, Exception ex) {
					
					Log.i("===", json.toString());
					if (json!=null && !json.isNull("result"))
					{
						String result;
						try {
							//[{"port":"10050","maintenance_type":"0","ipmi_privilege":"0","maintenance_from":"0","maintenances":[{"maintenanceid":"0"}],"ipmi_username":"","ipmi_disable_until":"0","ipmi_errors_from":"0","useip":"1","ipmi_ip":"","errors_from":"0","ipmi_password":"","ipmi_error":"","ipmi_port":"623","disable_until":"0","ipmi_authtype":"0","maintenanceid":"0","host":"A1","snmp_available":"0","error":"","status":"0","outbytes":"0","dns":"","available":"0","lastaccess":"0","ipmi_available":"0","useipmi":"0","inbytes":"0","snmp_disable_until":"0","ip":"10.30.200.1","maintenance_status":"0","hostid":"10025","proxy_hostid":"0","snmp_error":"","snmp_errors_from":"0"},{"port":"10050","maintenance_type":"0","ipmi_privilege":"0","maintenance_from":"0","maintenances":[{"maintenanceid":"0"}],"ipmi_username":"","ipmi_disable_until":"0","ipmi_errors_from":"0","useip":"1","ipmi_ip":"","errors_from":"0","ipmi_password":"","ipmi_error":"","ipmi_port":"623","disable_until":"0","ipmi_authtype":"0","maintenanceid":"0","host":"A13","snmp_available":"0","error":"","status":"0","outbytes":"0","dns":"","available":"0","lastaccess":"0","ipmi_available":"0","useipmi":"0","inbytes":"0","snmp_disable_until":"0","ip":"10.30.200.4","maintenance_status":"0","hostid":"10026","proxy_hostid":"0","snmp_error":"","snmp_errors_from":"0"},{"port":"10050","maintenance_type":"0","ipmi_privilege":"0","maintenance_from":"0","maintenances":[{"maintenanceid":"0"}],"ipmi_username":"","ipmi_disable_until":"0","ipmi_errors_from":"0","useip":"1","ipmi_ip":"","errors_from":"0","ipmi_password":"","ipmi_error":"","ipmi_port":"623","disable_until":"0","ipmi_authtype":"0","maintenanceid":"0","host":"A17","snmp_available":"1","error":"","status":"0","outbytes":"0","dns":"","available":"0","lastaccess":"0","ipmi_available":"0","useipmi":"0","inbytes":"0","snmp_disable_until":"0","ip":"10.30.200.3","maintenance_status":"0","hostid":"10027","proxy_hostid":"0","snmp_error":"","snmp_errors_from":"0"},{"port":"10050","maintenance_type":"0","ipmi_privilege":"0","maintenance_from":"0","maintenances":[{"maintenanceid":"0"}],"ipmi_username":"","ipmi_disable_until":"0","ipmi_errors_from":"0","useip":"1","ipmi_ip":"","errors_from":"0","ipmi_password":"","ipmi_error":"","ipmi_port":"623","disable_until":"0","ipmi_authtype":"0","maintenanceid":"0","host":"A6","snmp_available":"0","error":"","status":"0","outbytes":"0","dns":"","available":"0","lastaccess":"0","ipmi_available":"0","useipmi":"0","inbytes":"0","snmp_disable_until":"0","ip":"10.30.200.2","maintenance_status":"0","hostid":"10028","proxy_hostid":"0","snmp_error":"","snmp_errors_from":"0"},{"port":"10050","maintenance_type":"0","ipmi_privilege":"0","maintenance_from":"0","maintenances":[{"maintenanceid":"0"}],"ipmi_username":"","ipmi_disable_until":"0","ipmi_errors_from":"0","useip":"1","ipmi_ip":"","errors_from":"0","ipmi_password":"","ipmi_error":"","ipmi_port":"623","disable_until":"0","ipmi_authtype":"0","maintenanceid":"0","host":"B1","snmp_available":"0","error":"","status":"0","outbytes":"0","dns":"","available":"0","lastaccess":"0","ipmi_available":"0","useipmi":"0","inbytes":"0","snmp_disable_until":"0","ip":"10.30.200.5","maintenance_status":"0","hostid":"10029","proxy_hostid":"0","snmp_error":"","snmp_errors_from":"0"},{"port":"10050","maintenance_type":"0","ipmi_privilege":"0","maintenance_from":"0","maintenances":[{"maintenanceid":"0"}],"ipmi_username":"","ipmi_disable_until":"0","ipmi_errors_from":"0","useip":"1","ipmi_ip":"","errors_from":"0","ipmi_password":"","ipmi_error":"","ipmi_port":"623","disable_until":"0","ipmi_authtype":"0","maintenanceid":"0","host":"B13","snmp_available":"0","error":"","status":"0","outbytes":"0","dns":"","available":"0","lastaccess":"0","ipmi_available":"0","useipmi":"0","inbytes":"0","snmp_disable_until":"0","ip":"10.30.200.7","maintenance_status":"0","hostid":"10030","proxy_hostid":"0","snmp_error":"","snmp_errors_from":"0"},{"port":"10050","maintenance_type":"0","ipmi_privilege":"0","maintenance_from":"0","maintenances":[{"maintenanceid":"0"}],"ipmi_username":"","ipmi_disable_until":"0","ipmi_errors_from":"0","useip":"1","ipmi_ip":"","errors_from":"0","ipmi_password":"","ipmi_error":"","ipmi_port":"623","disable_until":"0","ipmi_authtype":"0","maintenanceid":"0","host":"B6","snmp_available":"0","error":"","status":"0","outbytes":"0","dns":"","available":"0","lastaccess":"0","ipmi_available":"0","useipmi":"0","inbytes":"0","snmp_disable_until":"0","ip":"10.30.200.6","maintenance_status":"0","hostid":"10031","proxy_hostid":"0","snmp_error":"","snmp_errors_from":"0"},{"port":"10050","maintenance_type":"0","ipmi_privilege":"0","maintenance_from":"0","maintenances":[{"maintenanceid":"0"}],"ipmi_username":"","ipmi_disable_until":"0","ipmi_errors_from":"0","useip":"1","ipmi_ip":"","errors_from":"0","ipmi_password":"","ipmi_error":"","ipmi_port":"623","disable_until":"0","ipmi_authtype":"0","maintenanceid":"0","host":"JF-CORE-52C-1","snmp_available":"1","error":"","status":"0","outbytes":"0","dns":"","available":"0","lastaccess":"0","ipmi_available":"0","useipmi":"0","inbytes":"0","snmp_disable_until":"0","ip":"10.20.30.1","maintenance_status":"0","hostid":"10032","proxy_hostid":"0","snmp_error":"","snmp_errors_from":"0"},{"port":"10050","maintenance_type":"0","ipmi_privilege":"0","maintenance_from":"0","maintenances":[{"maintenanceid":"0"}],"ipmi_username":"","ipmi_disable_until":"0","ipmi_errors_from":"0","useip":"1","ipmi_ip":"","errors_from":"0","ipmi_password":"","ipmi_error":"","ipmi_port":"623","disable_until":"0","ipmi_authtype":"0","maintenanceid":"0","host":"JF-CORE-52C-2","snmp_available":"1","error":"","status":"0","outbytes":"0","dns":"","available":"0","lastaccess":"0","ipmi_available":"0","useipmi":"0","inbytes":"0","snmp_disable_until":"0","ip":"10.20.30.2","maintenance_status":"0","hostid":"10033","proxy_hostid":"0","snmp_error":"","snmp_errors_from":"0"},{"port":"10050","maintenance_type":"0","ipmi_privilege":"0","maintenance_from":"0","maintenances":[{"maintenanceid":"0"}],"ipmi_username":"","ipmi_disable_until":"0","ipmi_errors_from":"0","useip":"1","ipmi_ip":"","errors_from":"0","ipmi_password":"","ipmi_error":"","ipmi_port":"623","disable_until":"0","ipmi_authtype":"0","maintenanceid":"0","host":"JF-HJ-28C-4","snmp_available":"1","error":"","status":"0","outbytes":"0","dns":"","available":"0","lastaccess":"0","ipmi_available":"0","useipmi":"0","inbytes":"0","snmp_disable_until":"0","ip":"10.20.30.6","maintenance_status":"0","hostid":"10034","proxy_hostid":"0","snmp_error":"","snmp_errors_from":"0"},{"port":"10050","maintenance_type":"0","ipmi_privilege":"0","maintenance_from":"0","maintenances":[{"maintenanceid":"0"}],"ipmi_username":"","ipmi_disable_until":"0","ipmi_errors_from":"0","useip":"1","ipmi_ip":"","errors_from":"0","ipmi_password":"","ipmi_error":"","ipmi_port":"623","disable_until":"0","ipmi_authtype":"0","maintenanceid":"0","host":"JF-HJ-28C-5","snmp_available":"1","error":"","status":"0","outbytes":"0","dns":"","available":"0","lastaccess":"0","ipmi_available":"0","useipmi":"0","inbytes":"0","snmp_disable_until":"0","ip":"10.20.30.13","maintenance_status":"0","hostid":"10035","proxy_hostid":"0","snmp_error":"","snmp_errors_from":"0"},{"port":"10050","maintenance_type":"0","ipmi_privilege":"0","maintenance_from":"0","maintenances":[{"maintenanceid":"0"}],"ipmi_username":"","ipmi_disable_until":"0","ipmi_errors_from":"0","useip":"1","ipmi_ip":"","errors_from":"0","ipmi_password":"","ipmi_error":"","ipmi_port":"623","disable_until":"0","ipmi_authtype":"0","maintenanceid":"0","host":"JF-HJ-28C-6","snmp_available":"1","error":"","status":"0","outbytes":"0","dns":"","available":"0","lastaccess":"0","ipmi_available":"0","useipmi":"0","inbytes":"0","snmp_disable_until":"0","ip":"10.20.30.26","maintenance_status":"0","hostid":"10036","proxy_hostid":"0","snmp_error":"","snmp_errors_from":"0"},{"port":"10050","maintenance_type":"0","ipmi_privilege":"0","maintenance_from":"0","maintenances":[{"maintenanceid":"0"}],"ipmi_username":"","ipmi_disable_until":"0","ipmi_errors_from":"0","useip":"1","ipmi_ip":"","errors_from":"0","ipmi_password":"","ipmi_error":"","ipmi_port":"623","disable_until":"0","ipmi_authtype":"0","maintenanceid":"0","host":"JF-HJ-28C-7-9","snmp_available":"1","error":"","status":"0","outbytes":"0","dns":"","available":"0","lastaccess":"0","ipmi_available":"0","useipmi":"0","inbytes":"0","snmp_disable_until":"0","ip":"10.20.30.38","maintenance_status":"0","hostid":"10037","proxy_hostid":"0","snmp_error":"","snmp_errors_from":"0"},{"port":"10050","maintenance_type":"0","ipmi_privilege":"0","maintenance_from":"0","maintenances":[{"maintenanceid":"0"}],"ipmi_username":"","ipmi_disable_until":"0","ipmi_errors_from":"0","useip":"1","ipmi_ip":"","errors_from":"0","ipmi_password":"","ipmi_error":"","ipmi_port":"623","disable_until":"0","ipmi_authtype":"0","maintenanceid":"0","host":"JF-HJ-28C-8-10","snmp_available":"1","error":"","status":"0","outbytes":"0","dns":"","available":"0","lastaccess":"0","ipmi_available":"0","useipmi":"0","inbytes":"0","snmp_disable_until":"0","ip":"10.20.30.42","maintenance_status":"0","hostid":"10038","proxy_hostid":"0","snmp_error":"","snmp_errors_from":"0"},{"port":"10050","maintenance_type":"0","ipmi_privilege":"0","maintenance_from":"0","maintenances":[{"maintenanceid":"0"}],"ipmi_username":"","ipmi_disable_until":"0","ipmi_errors_from":"0","useip":"1","ipmi_ip":"","errors_from":"0","ipmi_password":"","ipmi_error":"","ipmi_port":"623","disable_until":"0","ipmi_authtype":"0","maintenanceid":"0","host":"JF-HJ-52C-11","snmp_available":"1","error":"","status":"0","outbytes":"0","dns":"","available":"0","lastaccess":"0","ipmi_available":"0","useipmi":"0","inbytes":"0","snmp_disable_until":"0","ip":"192.168.11.2","maintenance_status":"0","hostid":"10039","proxy_hostid":"0","snmp_error":"","snmp_errors_from":"0"},{"port":"10050","maintenance_type":"0","ipmi_privilege":"0","maintenance_from":"0","maintenances":[{"maintenanceid":"0"}]...
							result = json.getString("result");
							List<ItemEntity> list = ItemEntity.getList(result);
							adapter.setItems(list);
							adapter.notifyDataSetChanged();
							// 设置选中
							listView.setSelection(0);
						} catch (JSONException e) {
							e.printStackTrace();
						}
					}else{
						makeMyToast(instance, getResources().getString(R.string.time_out));
					}
					
					if(proDialog!=null){
						proDialog.dismiss();
					}
				}
			});
			CommandService.addTaskAtFirst(cmd);
		}
	}
	
	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		ItemEntity h = (ItemEntity) arg0.getItemAtPosition(arg2);
		((TabGroupActivity) getParent()).startChildActivity("HistoryActivity", new Intent(getParent(),
				HistoryActivity.class).putExtra("itemId", h.getItemId()));
		
		/*ApplicationEntity h = (ApplicationEntity) arg0.getItemAtPosition(arg2);
		((TabGroupActivity) getParent()).startChildActivity("ItemActivity", new Intent(getParent(),
				ItemActivity.class).putExtra("application", h));*/
		
	}
	
	@Override
	public void onClick(View v) {
		
	}
	
	@Override
	public void onBackPressed() {
		//返回上一级
		finish();
	}
	
}
